package com.industrial.governance.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.domain.CityCount;
import com.industrial.governance.domain.PreMuImportDetailInfo;
import com.industrial.governance.domain.QueryParams;
import com.industrial.governance.domain.SysArea;
import com.industrial.governance.mapper.DataAcquisitionMapper;
import com.industrial.governance.mapper.PreMuImportDetailInfoMapper;
import com.industrial.governance.service.IDataAcquisitionService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DataAcquisitionServiceImpl implements IDataAcquisitionService {

    @Autowired
    private DataAcquisitionMapper dataAcquisitionMapper;

    @Autowired
    private PreMuImportDetailInfoMapper preMuImportDetailInfoMapper;

    /**
     * 查询数量
     *
     * @param queryParams
     */
    @Override
    public Map<String,Object> queryCount(QueryParams queryParams) {

        Map<String,Object> result = new HashMap<>();

        int cpCompanyCount = dataAcquisitionMapper.queryCpCompanyCount(queryParams);
        result.put("ycpCompanyCount",cpCompanyCount);
        if(StringUtils.equals("2023",queryParams.getYears())){
            Long dataImportCount = preMuImportDetailInfoMapper.queryDataImportCount(queryParams);
            result.put("dataImportCount",dataImportCount == null ? 0L : dataImportCount);
        }else{
            int dataImportCount = dataAcquisitionMapper.queryDataImportCount(queryParams);
            result.put("dataImportCount",dataImportCount);
        }
        return result;
    }

    /**
     * 查询各地市的数量
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryCityDataCj(QueryParams params) {

        //查询区域
        List<SysArea> areaList = dataAcquisitionMapper.selectAreaInfo(params);
        if(areaList == null || areaList.isEmpty()){
            throw new RuntimeException("没有查询到区域");
        }

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        if(StringUtils.equals(params.getYears(),"2023")){
            List<CityCount> cityCounts = preMuImportDetailInfoMapper.selectCityCount(params);
            for(CityCount temp : cityCounts){
                String areaName = temp.getAreaName();
                String count = temp.getCount();
                xAxis.add(areaName);
                yAxis.add(count);
            }
            Map<String,Object> result = new HashMap<>();
            result.put("xAxis",xAxis);
            result.put("yAxis",yAxis);
            return result;
        }

        //查询各地市统计局的数据
        List<CityCount> cityTjCount = dataAcquisitionMapper.selectCityTongJiDataCount(params);
        //查询各地市税务局的数据
        List<CityCount> citySwCount = dataAcquisitionMapper.selectCityShuiWuDataCount(params);
        //查询各地市人社局的数据
        List<CityCount> cityRsCount = dataAcquisitionMapper.selectCityRenSheDataCount(params);
        //查询各地市环保局的数据
        List<CityCount> cityHbCount = dataAcquisitionMapper.selectCityHuanBaoDataCount(params);
        //查询各地市国土局的数据
        List<CityCount> cityGtCount = dataAcquisitionMapper.selectCityGuoTuDataCount(params);

        //查询水利局的数据
        List<CityCount> cityShuLiCount = dataAcquisitionMapper.selectCityShuliDataCount(params);

        List<String> areaNameSet = areaList.stream().sorted(Comparator.comparing(k -> Integer.valueOf(k.getAreaCode()))).map(SysArea::getBaseName).collect(Collectors.toList());

        Map<String, String> shuiLiMap = new HashMap<>();
        if(cityShuLiCount != null && !cityShuLiCount.isEmpty()){
            shuiLiMap = cityShuLiCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        Map<String, String> tongJiMap = new HashMap<>();
        if(cityTjCount != null && !cityTjCount.isEmpty()){
            tongJiMap = cityTjCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        Map<String, String> shuiWuMap = new HashMap<>();
        if(citySwCount != null && !citySwCount.isEmpty()){
            shuiWuMap = citySwCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        Map<String, String> renSheMap = new HashMap<>();
        if(cityRsCount != null && !cityRsCount.isEmpty()){
            renSheMap = cityRsCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        Map<String, String> huanBaoMap = new HashMap<>();
        if(cityHbCount != null && !cityHbCount.isEmpty()){
            huanBaoMap = cityHbCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        Map<String, String> guoTuMap = new HashMap<>();
        if(cityGtCount != null && !cityGtCount.isEmpty()){
            guoTuMap = cityGtCount.stream().collect(Collectors.toMap(CityCount::getAreaName, CityCount::getCount));
        }

        for(String areaName : areaNameSet){

            String tempTongJiValue = StringUtils.isNotBlank(tongJiMap.get(areaName)) ? tongJiMap.get(areaName) : "0";
            String tempShuiWuValue = StringUtils.isNotBlank(shuiWuMap.get(areaName)) ? shuiWuMap.get(areaName) : "0";
            String tempRenSheValue = StringUtils.isNotBlank(renSheMap.get(areaName)) ? renSheMap.get(areaName) : "0";
            String tempHuanBaoValue = StringUtils.isNotBlank(huanBaoMap.get(areaName)) ? huanBaoMap.get(areaName) : "0";
            String tempGuoTuValue = StringUtils.isNotBlank(guoTuMap.get(areaName)) ? guoTuMap.get(areaName) : "0";
            String tempShuiLiValue = StringUtils.isNotBlank(shuiLiMap.get(areaName)) ? shuiLiMap.get(areaName) : "0";

            Integer tongJiInter = Integer.valueOf(tempTongJiValue);
            Integer shuiWuInter = Integer.valueOf(tempShuiWuValue);
            Integer renSheInter = Integer.valueOf(tempRenSheValue);
            Integer huanBaoInter = Integer.valueOf(tempHuanBaoValue);
            Integer guoTuInter = Integer.valueOf(tempGuoTuValue);
            Integer shuiLiInter = Integer.valueOf(tempShuiLiValue);

            Integer resultValue = tongJiInter + shuiWuInter + renSheInter + huanBaoInter + guoTuInter + shuiLiInter;


            //只放入大于0的数据
            if (resultValue > 0) {
                xAxis.add(areaName);
                yAxis.add(String.valueOf(resultValue));
            }
        }



        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }

    /**
     * 查询年份的数据数量
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryYearDataCj(QueryParams params) {

        Integer preFiveYear = Integer.parseInt(params.getYears()) - 4;
        params.setPreFiveYers(String.valueOf(preFiveYear));

        //查询各地市统计局的数据
        List<CityCount> cityTjCount = dataAcquisitionMapper.selectYearsTongJiDataCount(params);
        //查询各地市税务局的数据
        List<CityCount> citySwCount = dataAcquisitionMapper.selectYearsShuiWuDataCount(params);
        //查询各地市人社局的数据
        List<CityCount> cityRsCount = dataAcquisitionMapper.selectYearsRenSheDataCount(params);
        //查询各地市环保局的数据
        List<CityCount> cityHbCount = dataAcquisitionMapper.selectYearsHuanBaoDataCount(params);
        //查询各地市国土局的数据
        List<CityCount> cityGtCount = dataAcquisitionMapper.selectYearsGuoTuDataCount(params);
        //查询水利局的数据
        List<CityCount> cityShuLiCount = dataAcquisitionMapper.selectYearsCityShuliDataCount(params);


        Map<String, String> shuiLiMap = new HashMap<>();
        if(cityShuLiCount != null && !cityShuLiCount.isEmpty()){
            shuiLiMap = cityShuLiCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        Map<String, String> tongJiMap = new HashMap<>();
        if(cityTjCount != null && !cityTjCount.isEmpty()){
            tongJiMap = cityTjCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        Map<String, String> shuiWuMap = new HashMap<>();
        if(citySwCount != null && !citySwCount.isEmpty()){
            shuiWuMap = citySwCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        Map<String, String> renSheMap = new HashMap<>();
        if(cityRsCount != null && !cityRsCount.isEmpty()){
            renSheMap = cityRsCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        Map<String, String> huanBaoMap = new HashMap<>();
        if(cityHbCount != null && !cityHbCount.isEmpty()){
            huanBaoMap = cityHbCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        Map<String, String> guoTuMap = new HashMap<>();
        if(cityGtCount != null && !cityGtCount.isEmpty()){
            guoTuMap = cityGtCount.stream().collect(Collectors.toMap(CityCount::getYear, CityCount::getCount));
        }

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        int i2 = 0;
        for(int i = preFiveYear; i <= Integer.valueOf(params.getYears()); i++,i2++){
            xAxis.add(String.valueOf( preFiveYear.intValue()+ i2));

        }

        for(String year : xAxis){
            String tempTongJiValue = StringUtils.isNotBlank(tongJiMap.get(year)) ? tongJiMap.get(year) : "0";
            String tempShuiWuValue = StringUtils.isNotBlank(shuiWuMap.get(year)) ? shuiWuMap.get(year) : "0";
            String tempRenSheValue = StringUtils.isNotBlank(renSheMap.get(year)) ? renSheMap.get(year) : "0";
            String tempHuanBaoValue = StringUtils.isNotBlank(huanBaoMap.get(year)) ? huanBaoMap.get(year) : "0";
            String tempGuoTuValue = StringUtils.isNotBlank(guoTuMap.get(year)) ? guoTuMap.get(year) : "0";
            String tempShuiLiValue = StringUtils.isNotBlank(shuiLiMap.get(year)) ? shuiLiMap.get(year) : "0";

            Integer tongJiInter = Integer.valueOf(tempTongJiValue);
            Integer shuiWuInter = Integer.valueOf(tempShuiWuValue);
            Integer renSheInter = Integer.valueOf(tempRenSheValue);
            Integer huanBaoInter = Integer.valueOf(tempHuanBaoValue);
            Integer guoTuInter = Integer.valueOf(tempGuoTuValue);
            Integer shuiLiInter = Integer.valueOf(tempShuiLiValue);

            Integer resultValue = tongJiInter + shuiWuInter + renSheInter + huanBaoInter + guoTuInter + shuiLiInter;


            //如果是2023年则用统计表
            if(StringUtils.equals(year,"2023")){
//                QueryParams queryParams = new QueryParams();
//                queryParams.setYears("2023");
//                if(params != null && params.getCounty() != null){
//                    queryParams.setCounty(params.getCounty());
//                }
//                if(params != null && params.getCity() != null){
//                    queryParams.setCity(params.getCity());
//                }
//                if(params != null && params.getProvince() != null){
//                    queryParams.setProvince(params.getProvince());
//                }\
                Long l = preMuImportDetailInfoMapper.queryDataImportCount(params);
                long dataImportCount = l == null ? 0L : l;
                yAxis.add(String.valueOf(dataImportCount));
            }else{
                yAxis.add(String.valueOf(resultValue));
            }
        }

        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }
}
